const {
  Mysql,
  addSql,
  xiuSql,
  remSql,
  execute,
  paraphrase,
} = require("../../db/db");
// 购物车页返回数据(测试)
let getCarData = async (ctx, next) => {
  let { uid } = ctx.request.body;
  console.log(ctx.request.body);
  //用来存储商品总价
  let price = 0;
  //用来储存选中的数量
  let num = 0;
  let res = await Mysql("shopping", { uid });
  //只计算价格
  // 先把当前用户所有的商品拿到
  // 计算所有商品总价
  paraphrase(res).forEach((item, index) => {
    //判断如果该商品为选中状态累加 数量 与 价格
    console.log(item.goods_checked);
    if (JSON.parse(item.goods_checked)) {
      //总价
      price += item.goods_number * item.price;
      //总数量
      num += item.goods_number;
    }
  });
  console.log(price);
  ctx.body = {
    msg: "请求成功",
    data: paraphrase(res),
    code: "02",
    price: price.toFixed(2),
    num,
    goods_num: paraphrase(res).length,
  };
  await next();
};

// 购物车选中商品时 修改商品选中状态
let shopp = async (ctx, next) => {
  let { goods_checked, goods_id } = ctx.request.body;
  console.log(ctx.request.body);
  let res = await xiuSql(
    "shopping",
    { goods_checked: !goods_checked },
    { goods_id: goods_id }
  );
  ctx.body = {
    msg: "修改成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 购物车全选反选
let chooseShop = async (ctx, next) => {
  // 接收全选状态
  let { val, uid } = ctx.request.body;
  console.log(ctx.request.body);
  //修改全部商品状态为全选的状态
  await execute(
    `UPDATE shopping SET goods_checked=REPLACE(goods_checked,'${!val}','${val}')`
  );
  //   并返回所有数据
  let res = await Mysql("shopping", { uid });
  ctx.body = {
    msg: "修改成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 购物车删除/移入收藏夹/加减/计算价格接口
let changeShopsTypes = async (ctx, next) => {
  // 接收当前用户id / 商品id
  let { type, uid, id, result, flag } = ctx.request.body;
  console.log(ctx.request.body);
  let res = [];
  //判断点击的是删除/移入收藏夹/加减 还是只计算价格
  if (type === "01") {
    //删除
    //根据用户id找出当前用户购物车商品，
    //再删除要删除的商品
    result.forEach(async (item, index) => {
      await execute(
        `DELETE FROM shopping WHERE uid=${uid} and goods_id=${item}`
      );
    });
  } else if (type === "02") {
    //移入收藏夹
    result.forEach(async (item, index) => {
      res = await execute(
        `select * from likes where uid=${uid} and goods_id=${item}`
      );
      // // 判断该商品有没有收藏过
      if (paraphrase(res).length) {
        // 有
        ctx.body = {
          msg: "该商品已经收藏过啦—_—|",
          code: "01",
        };
        return;
      } else {
        //没有
        //添加该商品id到收藏的数据库
        await addSql("likes", { uid, goods_id: item });
        // 并删除该商品在购物车的数据
        await execute(
          `DELETE FROM shopping WHERE uid=${uid} and goods_id=${item}`
        );
      }
    });
    ctx.body = {
      msg: "收藏成功",
      code: "02",
    };
  } else if (type === "03") {
    //商品加减
    let shopping_id = await Mysql("shopping", { goods_id: id });
    // 判断点击的是加/减
    if (flag) {
      // 加
      await execute(
        `UPDATE shopping set goods_number=${
          paraphrase(shopping_id)[0].goods_number + 1
        } WHERE uid=${uid} and goods_id=${id}`
      );
    } else {
      // 减
      if (paraphrase(shopping_id)[0].goods_number > 1) {
        await execute(
          `UPDATE shopping set goods_number=${
            paraphrase(shopping_id)[0].goods_number - 1
          } WHERE uid=${uid} and goods_id=${id}`
        );
      } else {
        ctx.body = {
          msg: "已经没有商品可减咯；kkkkkkkkk",
          code: "02",
          price,
          num,
        };
      }
    }
  }

  ctx.body = {
    msg: "请求成功",
    code: "02",
  };
  await next();
};

//商品详情页添加购物车
let addshopping = async (ctx, next) => {
  // 接收全选状态
  let { data, uid } = ctx.request.body;
  console.log(ctx.request.body);
  let res = [];
  res = await execute(
    `SELECT * from shopping where uid=${uid} and goods_id=${data.goods_id}`
  );
  console.log(paraphrase(res));
  if (paraphrase(res).length) {
    // 已购买过
    await execute(
      `UPDATE shopping SET goods_number=${
        paraphrase(res)[0].goods_number + 1
      } WHERE uid=${uid} and goods_id=${data.goods_id}`
    );
  } else {
    // 没够买过
    res = await addSql("shopping", {
      goods_id: data.goods_id,
      goods_typeid: data.goods_typeid,
      goods_name: data.goods_name,
      uid,
      goods_express: data.goods_express,
      goods_url: data.goods_url,
      price: data.price,
      goods_original: data.goods_original,
      goods_people: data.goods_people,
      goods_volume: data.goods_volume,
      goods_evaluate: data.goods_evaluate,
      goods_checked: data.goods_checked,
      goods_number: data.goods_number,
      stock_num: data.stock_num,
      collection_id: data.collection_id,
      hide_stock: data.hide_stock,
      num1: 0,
    });
  }
  ctx.body = {
    msg: "添加成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
module.exports = {
  getCarData,
  shopp,
  chooseShop,
  changeShopsTypes,
  addshopping,
};
